package com.kylin.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kylin.common.core.beans.ResultBean;
import com.kylin.common.core.exception.ServiceException;
import com.kylin.common.core.validator.Assert;
import com.kylin.common.redis.service.RedisService;
import com.kylin.common.security.service.CaptchaService;
import com.kylin.common.security.util.SecurityUtils;
import com.kylin.system.entity.SysUser;
import com.kylin.system.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 登录相关接口
 * @author wuhao
 * @version 1.0 - 2023/1/13
 */
@RestController
@RequestMapping("")
public class SysLoginController {

    @Autowired
    private CaptchaService captchaService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private RedisService redisService;
    @Autowired
    private SysUserService userService;

    /**
     * 获取验证码
     * @return
     * @author wuhao
     */
    @RequestMapping("/code")
    public ResultBean<Object> getCode() {
        return captchaService.createCaptcha();
    }


    /**
     * 修改个人信息
     * @return
     * @author wuhao
     * @version 1.0.0 - 2024/11/25
     */
    @PutMapping("/user/modify")
    public ResultBean<Object> modifyInfo(@RequestBody SysUser user) {
        userService.getBaseMapper().update(null , new UpdateWrapper<SysUser>()
                .set("nick_name", user.getNickName())
                .set("sex", user.getSex())
                .set(StringUtils.isNotEmpty(user.getPhone()),"phone", user.getPhone())
                .set(StringUtils.isNotEmpty(user.getEmail()),"email", user.getEmail())
                .eq("id", SecurityUtils.getUserId()));
        return ResultBean.onSuccess();
    }

    /**
     * 修改密码
     * @return
     * @author wuhao
     * @version 1.0.0 - 2024/11/25
     */
    @PostMapping("/user/modify/password")
    public ResultBean<Object> modifyPassword(@RequestBody JSONObject params) {
        String oldPassword = params.getString("oldPassword");
        String newPassword = params.getString("password");
        Assert.isBlank("密码不能为空", oldPassword, newPassword);
        // 1.验证旧密码是否正确
        SysUser user = userService.getById(SecurityUtils.getUserId());
        if(!SecurityUtils.matchesPassword(oldPassword, user.getPassword())) {
            throw new ServiceException("旧密码不正确");
        }
        userService.resetUserPwd(user.getId(), SecurityUtils.encryptPassword(newPassword));
        return ResultBean.onSuccess();
    }


    /**
     * 退出登录
     * @return
     * @author wuhao
     */
//    @GetMapping("logout")
//    public ResultBean<Object> logout() {
//        // 1.获取Token字符串，token 置于 header 里
//        String token = request.getHeader("token");
//        if (!StringUtils.hasText(token)) {
//            token = request.getParameter("token");
//        }
//        Claims claims = JwtUtils.parseJwt(token);
//        ClaimsInfo claimsInfo = JSON.parseObject(claims.getSubject(), ClaimsInfo.class);
//        redisService.deleteObject(claimsInfo.getTokenKey());
//        return ResultBean.onSuccess();
//    }

}
